﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.Services/WindowService
* 类       名: WindowService
* 功      能:  TD
* 时      间:  2024/2/23 15:32:56
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using DryIoc;
using Prism.Ioc;
using System.Collections.Generic;
using System.Windows;
using XrayDirection.DAL.DataEntity;
using XrayDirection.ViewModels;
using XrayDirection.Views;

namespace XrayDirection.Services
{
    public class WindowService : IWindowService
    {
        private readonly IContainerProvider _containerProvider;
        private readonly Dictionary<string, Window> OpenWindows = new Dictionary<string, Window>();

        public WindowService(IContainerProvider containerProvider)
        {
            _containerProvider = containerProvider;
        }

        public void CloseWindow(string windowName)
        {
            if (OpenWindows.ContainsKey(windowName))
            {
                Window closeWindow = OpenWindows[windowName];
                closeWindow.Close();
                OpenWindows.Remove(windowName);
            }
        }

        public void ShowWindow(string windowName)
        {
            if (windowName == nameof(InputNextRecipeView))
            {
                // 输入下一个工单的弹窗
                MainView window = _containerProvider.Resolve<MainView>();
                InputNextRecipeView inputNextRecipeView = new InputNextRecipeView();
                // 这里不能再app.cs文件中进行注册,因为这里不是通过导航来打开的窗口,没办法建立关联
                inputNextRecipeView.DataContext = _containerProvider.Resolve<InputNextRecipeViewModel>();
                OpenWindows[nameof(InputNextRecipeView)] = inputNextRecipeView;
                /*
                 * Owner属性主要有以下作用:
                 * 1) 模态行为: 当你调用ShowDialog()来显示一个窗口的时候,如何设置饿了这个窗口的Owner,那么这个窗口将会以模态的方式显示.
                 *                   这个窗口必须处理完,才会返回到主窗口
                 * 2) 所有者窗口始终在下面
                 * 
                 * 3) 关闭行为: 当关闭owner窗口时,其所拥有的所有窗口会自动关闭
                 */
                inputNextRecipeView.Owner = window;
                inputNextRecipeView.ShowDialog();
            }
            else if (windowName == nameof(ChangeNextRecipeView))
            {
                // 切换下一个工单的窗口
                MainView window = _containerProvider.Resolve<MainView>();
                ChangeNextRecipeView changeNextRecipeView = new ChangeNextRecipeView();
                changeNextRecipeView.DataContext = _containerProvider.Resolve<ChangeNextRecipeViewModel>();
                // 刷新一下数据
                _containerProvider.Resolve<ChangeNextRecipeViewModel>().RefreshInputPlanOrders();
                OpenWindows[nameof(ChangeNextRecipeView)] = changeNextRecipeView;
                changeNextRecipeView.Owner = window;
                changeNextRecipeView.ShowDialog();
            }
            else if (windowName == nameof(EditRecipeView))
            {
                // 切换下一个工单窗口
                MainView window = _containerProvider.Resolve<MainView>();
                EditRecipeView editRecipeView = new EditRecipeView();
                // 关联View和ViewModel
                editRecipeView.DataContext = _containerProvider.Resolve<EditRecipeViewModel>();
                // 刷新一下数据
                _containerProvider.Resolve<EditRecipeViewModel>().RefreshEditOrder();
                OpenWindows[nameof(EditRecipeView)] = editRecipeView;
                editRecipeView.Owner = window;
                editRecipeView.ShowDialog();
            }
        }


    }
}
